#!/usr/bin/env python

"""
Functions: see README
VERBOSE
"""

import hashlib
import json
import math
import random
import string
import sys
import time

try: # Use psyco if available
	import psyco
	psyco.full()
except ImportError:
	pass

LENFORCOIILIDE = input('number of hexis to collide (full length 64 for SHA256)')
print LENFORCOIILIDE, "full image:", 16**LENFORCOIILIDE,  "expected security:", 4**LENFORCOIILIDE
Number_of_rounds_to_run = input('number of - birthdays - on each side: ')

outputfiler = input('outputfiler name: ')
outputfilek = input('outputfilek name: ')

VERBOSE = 10
SPLITER = '-'

add_hex2 = lambda hex1, hex2:  (str(hex(int(hex1, 16) + int(hex2, 16))))[-len(hex1):]

"""when last symbol is expected to be L"""
add_long_hex2 = lambda hex1, hex2:  (str(hex(int(hex1, 16) + int(hex2, 16))))[-LENFORCOIILIDE-1:-1]

#print add_hex2('ff', 'ff')


start = time.clock()


def Random_Oracle(message):
	return hashlib.sha256(message).hexdigest()

def Add_Random_Oracle(message1, message2):
	return hashlib.sha256(message1).hexdigest()
#	return hashlib.sha256(message).hexdigest()


def querying_random_oracle(number):
	randomness = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(10)])
	value = Random_Oracle("1alo 2vet 3 ute")
	print value,randomness
	return value,randomness

	


def main():

	#Ta = eval(open("hash_database_new").read())

	Ta = ravlyuchenko_s_table({})

	print time.clock() - start

	kalnova_s_table(Ta)

	print time.clock() - start

#	print>>file("hash_database", 'w'), Ta


def ravlyuchenko_s_table(T):

#	print T

	collides = 0
	events = []
	for x in xrange(0, Number_of_rounds_to_run):
		randomness1 = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(10)])
		
		tar = Random_Oracle(randomness1)[-LENFORCOIILIDE:]

		if tar in T:
			T[tar].append(randomness1)
			collides += 1
			print collides, "omg!!!!!!!!! collides!!!", tar, T[tar]
			events.append(T[tar])

			
		else:
			T[tar]=[randomness1]
		if x%100000 == 0:
			print collides, x, tar, T[tar]
#	print T
	if len(events)>0: print>>file(outputfiler, 'w'), collides, events
	return T

def kalnova_s_table(T):

#	print T
	collides = 0
	events = []
	for x in xrange(0, Number_of_rounds_to_run):
		randomness1 = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(10)])
		randomness2 = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(10)])
		
#		randomness = '8lNt8myRrT'

		tar = add_long_hex2(Random_Oracle(randomness1), Random_Oracle(randomness2))

		if tar in T:
			T[tar].append([randomness1, randomness2])
			collides += 1
			print collides, "omg!!!!!!!!! collides!!!", tar, T[tar]
			events.append(T[tar])

			
		else:
			T[tar]=[[randomness1, randomness2]]
		if x%100000 == 0:
			print collides, x, Random_Oracle(randomness1), Random_Oracle(randomness2), tar, T[tar]
	
	if len(events)>0: print>>file(outputfilek, 'w'), collides, events
	



print Random_Oracle('JEh8qSYF0L')

def verify():
	tar1 = add_hex2(Random_Oracle('ZhecXGmS4m'), Random_Oracle('sqAo7QkyOK'))
	tar2 = add_hex2(Random_Oracle('PncgEHa51a'), Random_Oracle('hoPkJAmgUN'))
	print tar1, tar2

main()

print time.clock() - start
